<template>
	<view class="cz" id="cz">
		<view class="nock" v-if="!ckcode">
			<view class="title">
				
				<van-notice-bar
				  left-icon="https://img.yzcdn.cn/1.png"
				  text="首次充值198元及以上立即成为创客!立享分润!"
				/>
			</view>
			
			<button type="primary" @click="miniPay">成为创客</button>
		</view>
		
		<view class="qcode" v-else>
			<view class="bar">
				<van-notice-bar
				  text="尊敬的会员,您已经成为创客,快分享您的小程序码邀请更多的朋友吧!"
				  speed="50"
				  left-icon="//img.yzcdn.cn/public_files/2017/8/10/6af5b7168eed548100d9041f07b7c616.png"
				/>
			</view>
			<view class="imgbox" style="width: 100%;padding-top: 20%;">
				<image :src="ckcode" mode="" @longtap="shareImg(ckcode)" ></image>
				<button type="primary" style="margin-top: 50rpx;" @click="saveCode">保存到相册</button>
				<!-- <button type="primary" @click="scanCode">扫一扫</button> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		computed: {
			token() {
				return this.$store.state.token
			},
			openid() {
				return this.$store.state.sid.openid
			},
			ckcode() {
				return this.$store.state.ckcode
			},
		},
		data() {
			return {
				imgsrc: "",
			}
		},
		onLoad() {

		},
		onShow() {
			// this.isChuangke()
		},
		methods: {
			// 扫码
			scanCode() {
				uni.scanCode({
					success( res ) {
						console.log(res);
					}
				})
			},
			
			//  分享图片
			shareImg( src ) {
				console.log(1);
				let imgArr = []
				imgArr.push(src)
				uni.previewImage({
					urls: imgArr,
					current: src,
					success( res ) {
						console.log(res);
						console.log(imgArr);
					}
				})
			},
			// 保存二维码到相册
			saveCode() {
				let self = this
				console.log(self.ckcode);
				if(self.ckcode) {
					uni.downloadFile({
						url: self.ckcode,
						success( res ) {
							console.log(res);
							uni.saveImageToPhotosAlbum({
								filePath:res.tempFilePath,
								success( msg ) {
									console.log(msg);
									let status = msg.errMsg
									if(status == "saveImageToPhotosAlbum:ok") {
										uni.showToast({
											title: "保存成功",
											icon: "success"
										})
									}
								}
							})
						},
						fail( err) {
							console.log(err);
						}
					})
				}
			},
			// 获取access_token
			async getAccessToken() {
				let res = await this.$Request("POST","/base/getAccessToken", "")
				//console.log(res)
				if(res.data) {
					this.$store.commit("getAccess_token", res.data)
				}
				// console.log(this.$store.state.Access_token);
			},
			
			//  查看是否是创客
			async isChuangke() {
				let res = await this.$Request("POST", "/Index/makerorder",{ token: this.token })
				this.ckcode = res.data.data
				console.log(this.imgsrc);
			},
			// node
			testpay() {
				let self = this
				uni.request({
					url: "https://www.awoojing.club/users/wxpay",
					method:"POST",
					data:{
						openid: self.openid,
						money: 0.01
					},
					success(payorder) {
						 console.log(payorder);
						let data = {
							timeStamp: String(payorder.data.timeStamp) ,
							nonceStr: payorder.data.nonceStr,
							package: payorder.data.package,
							paySign: payorder.data.paySign,
						}
						console.log(data);
						uni.requestPayment({
							timeStamp: String(payorder.data.timeStamp) ,
							nonceStr: payorder.data.nonceStr,
							package: payorder.data.package,
							paySign: payorder.data.paySign,
							signType:"MD5",
							success(e) {
								console.log(e);
							},
							fail(err) {
								console.log(err);
							}
						})
					},
				})
			},
			
			
			// 发起微信支付
			async miniPay() {
				let timeStamp = Math.floor(new Date().getTime() / 1000 )
				let order = await this.$Request("POST", "/order/makerorder", {token: this.token })
				// console.log(order);
				
					uni.requestPayment({
						timeStamp: order.data.timeStamp,
						nonceStr: order.data.nonceStr,
						package: order.data.package ,
						paySign: order.data.paySign,
						signType:"MD5",
						success(e) {
							console.log(e);
							let status = e.errMsg
							if(status == "requestPayment:ok") {
								uni.showToast({
									title: "支付成功",
									icon:"success"
								})
							}
						},
						fail(err) {
							console.log(err);
						}
					})
			},
		},
		
		}
	
</script>

<style scoped lang="scss">
	#cz {
		width: 100%;
		box-sizing: border-box;
		padding: 20rpx 30rpx;
		text-align: center;
	}
	.qcode {
		width: 100%;
		image {
			width: 400rpx;
			height: 400rpx;
		};
	}
</style>
